#ifndef _GLFONT_EXT_H_
#define _GLFONT_EXT_H_

#include <stdlib.h>
#include <memory.h>

typedef enum GLFTmode_tag {
  GLFT_GM_LEFTUP,  // Zero: default
  GLFT_GM_CENTER
} GLFTmode;

struct Vec3;
struct Coord;

struct TextLine
{
  TextLine() {
    Reset();
  }
  ~TextLine() {
    Release();
  }

  float GetWidth() const { return charNum*widthUnit; }
  float GetHeight() const { return heightUnit; }

  bool Generate(float x0, float y0, char* initStr, GLFTmode mode =GLFT_GM_LEFTUP);
  void ChangeText(char* str);

  void Draw();

  static bool UseTextureUnit(unsigned int glTexUnit, unsigned int glTexId);
  static bool Init(unsigned int dx, unsigned int dy, float fw, float fh);
  static void Shutdown();

  static float GetCellWidth() { return cellX; }
  static float GetCellHeight() { return cellY; }
private:
  static float cellX;
  static float cellY;
  static float widthUnit;
  static float heightUnit;

  static void FontGenLineGeom(TextLine& txt, unsigned int cellNum, GLFTmode m, float x0, float y0);
  static void FontGenCharUVs(TextLine& txt);

  void Reset() {
    memset(this,0,sizeof(*this));
  }
  void ReleaseVertex() {
    if(vertex) free(vertex);
    vertex = 0;
    vertexNum = 0;
  }
  void ReleaseCoord() {
    if(coord) free(coord);
    coord = 0;
    coordNum = 0;
  }
  void Release() {
    if(vertex) free(vertex);
    if(index) free(index);
    if(coord) free(coord);
    Reset();
  }

  Vec3* vertex;
  int vertexNum;
  short* index;
  int indexNum;
  Coord* coord;
  int coordNum;

  int charNum;
  char* str;

  GLFTmode mode;
  float x0, y0;
};




#endif  // _GLFONT_EXT_H_
